/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chating;
import java.sql.SQLException;
import java.util.ArrayList;
import database.Database;
import java.awt.Container;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
/**
 *
 * @author Andi
 */
public class User {
    // attributes
    private int userID;
    private String userName;
    private String password;
    private String fullName;
    private String email;
    private String role;
    private boolean ban;
    private int maxRooms;
    private ArrayList<String> friends;
    private ArrayList<String> bannedRoom;
    private Database db = new Database();
    private long lastRequest;

    // constructor
    public User(){
        this.friends = new ArrayList<String>();
        this.bannedRoom = new ArrayList<String>();
    }
    public User(User u){
        this.userID = u.getUserID();
        this.userName = u.getUserName();
        this.password = u.getPassword();
        this.fullName = u.getFullName();
        this.email = u.getEmail();
        this.role = u.getRole();
        this.ban = u.IsBan();
        this.friends = new ArrayList<String>(u.getFriends());
        this.bannedRoom = new ArrayList<String>(u.getBannedRoom());
    }
    public User(int userID, String userName, String password, String fullName, String email, String role, boolean isBan, ArrayList<String> friends){
        this.userID = userID;
        this.userName = userName;
        this.password = password;
        this.fullName = fullName;
        this.email = email;
        this.role = role;
        this.ban = isBan;
        this.friends = new ArrayList<String>(friends);
        this.bannedRoom = new ArrayList<String>();
    }

    // getter and setter
    /**
     * @return the userID
     */
    public int getUserID() {
        return userID;
    }

    /**
     * @param userID the userID to set
     */
    public void setUserID(int userID) {
        this.userID = userID;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the fullName
     */
    public String getFullName() {
        return fullName;
    }

    /**
     * @param fullName the fullName to set
     */
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the role
     */
    public String getRole() {
        return role;
    }

    /**
     * @param role the role to set
     */
    public void setRole(String role) {
        this.role = role;
    }

    /**
     * @return the isBan
     */
    public boolean IsBan() {
        return ban;
    }

    /**
     * @param isBan the isBan to set
     */
    public void setBan(boolean isBan) {
        this.ban = isBan;
    }

    /**
     * @return the maxRooms
     */
    public int getMaxRooms() {
        return maxRooms;
    }

    /**
     * @param maxRooms the maxRooms to set
     */
    public void setMaxRooms(int maxRooms) {
        this.maxRooms = maxRooms;
    }

    /**
     * @return the friends
     */
    public ArrayList<String> getFriends() {
        return friends;
    }

    /**
     * @param friends the friends to set
     */
    public void setFriends(ArrayList<String> friends) {
        this.friends = friends;
    }

    /**
     * @return the bannedRoom
     */
    public ArrayList<String> getBannedRoom() {
        return bannedRoom;
    }

    /**
     * @param bannedRoom the bannedRoom to set
     */
    public void setBannedRoom(ArrayList<String> bannedRoom) {
        this.bannedRoom = bannedRoom;
    }


    /**
     * @return the db
     */
    public Database getDb() {
        return db;
    }

    /**
     * @param db the db to set
     */
    public void setDb(Database db) {
        this.db = db;
    }

    /**
     * @return the lastRequest
     */
    public long getLastRequest() {
        return lastRequest;
    }

    /**
     * @param lastRequest the lastRequest to set
     */
    public void setLastRequest(long lastRequest) {
        this.lastRequest = lastRequest;
    }

    // methods
    public int getIndexBannedRoom(String roomName){
        int i;
        for(i=0; i<this.bannedRoom.size(); i++){
            if(this.bannedRoom.get(i).equalsIgnoreCase(roomName)){
                break;
            }
        }
        if(i<this.getBannedRoom().size()){
            return i;
        }else{
            return -1;
        }
    }
    
    public boolean isBannedInRoom(String roomName){
        boolean banRoom=false;
        int i= this.getIndexBannedRoom(roomName);
        if (i>=0){
            banRoom = true;
        }
        return banRoom;
    }

    public void addFriends(String friendName){
        this.friends.add(friendName);
        try {
            db.addFriend(this.userName, friendName);
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean isOnline(){
        if(System.currentTimeMillis()-this.lastRequest < 5000){
            return true;
        }else{
            return false;
        }
    }
//    public ArrayList<String> showOnlineFriends(DataContainer cont){
//        ArrayList<String> onlineFriends = new ArrayList<String>();
//        for (int i=0; i<this.friends.size(); i++){
//            if (cont.getUserByName(this.friends.get(i)).IsOnline()){
//                onlineFriends.add(this.friends.get(i));
//            }
//        }
//        return onlineFriends;
//    }

    public boolean isModerator(){
        return (this.role.equalsIgnoreCase("moderator"));
    }

    public boolean isAdmin(){
        return (this.role.equalsIgnoreCase("admin"));
    }

    public int getNumRoomCreated(DataContainer cont){
        int num=0;
        for(int i=0; i<cont.getRooms().size(); i++){
            if(cont.getRooms().get(i).getOwner().equalsIgnoreCase(this.userName)){
                num++;
            }
        }
        return num;
        
    }

    public boolean createRoom(DataContainer cont, String roomName, String category, String subCategory, boolean persist){
        if (this.isModerator()){
            Room r = new Room(roomName, category, subCategory, this.userName, persist);
            cont.addRoom(r);
            return true;
        }else{
            if (this.maxRooms >= this.getNumRoomCreated(cont)){
                Room r = new Room(roomName, category, subCategory, this.userName, persist);
                cont.addRoom(r);
                return true;
            }else{
                return false;
            }            
        }
    }

    public boolean joinRoom(DataContainer cont, String roomName){
        if(!this.isBannedInRoom(roomName)){
            for (int i=0; i<cont.getRooms().size(); i++){
                if (cont.getRooms().get(i).getNameRoom().equalsIgnoreCase(roomName)){
                    cont.getRooms().get(i).addMember(this.userName);
                    break;
                }
            }
            return true;
        }else{
            return false;
        }
    }

    public void exitRoom(DataContainer cont, String roomName){
        for (int i=0; i<cont.getRooms().size(); i++){
            if (cont.getRooms().get(i).getNameRoom().equalsIgnoreCase(roomName)){
                cont.getRooms().get(i).removeMember(this.userName);
                break;
            }
        }
    }

    public ArrayList<Room> showRoomEntered(DataContainer cont){
        ArrayList<Room> r = new ArrayList<Room>();
        for(int i=0; i<cont.getRooms().size(); i++){
            for(int j=0; j<cont.getRooms().get(i).getMembers().size(); j++){
                if (cont.getRooms().get(i).getMembers().get(j).getUserName().equalsIgnoreCase(this.userName)){
                    r.add(cont.getRooms().get(i));
                    break;
                }
            }
        }
        return r;
    }

    public void postChat(Room room, String contentPost){
        room.addMessage(this.userName, contentPost);
    }

    public String getInfoUser(DataContainer cont, String userName){
        User u= new User(cont.getUserByName(userName));
        String s="";
        try {
            s = "Nama = " + getDb().getFullNameUser(userName) + "\nUsername = " + userName + "\nRole = " + getDb().getRoleUser(userName) + "\n";
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return s;
    }
    



}
